package com.sky.classLoader;

import java.io.File;
import java.net.URL;

/**
 * @author : wushikai
 * <p>
 * date : 2022-05-06
 */
class Client {


    private static void getFile() {


        URL resource = Client.class.getClassLoader().getResource("org/apache/catalina/mbeans-descriptors.xml");
        if (resource != null) {
            System.out.println(resource.getFile());
            File file = new File(resource.getFile());
            System.out.println("file.exists() = " + file.exists());
        } else {

            System.out.println("f*ck you!!!!!!");
        }


    }


    private static void getPath() {


        System.out.println(System.getProperty("java.class.path"));  //加载类的路径,  包括jdk 的, 包括这个项目本身的;

        System.out.println(System.getProperty("user.dir"));

        System.out.println(Client.class.getClassLoader().getResource("").getPath());

        System.out.println(new File(Client.class.getResource("/").getPath()));

        System.out.println(new File("").getAbsolutePath());

    }


    /**
     * 重新写一个 classloader , 就可以打破 双亲委派机制 ;
     */
    private static void classLoaderClass() {

        ClassLoader classLoader = Client.class.getClassLoader();

        System.out.println("classLoader.toString() = " + classLoader.toString());

        System.out.println("classLoader.getParent() = " + classLoader.getParent());

        System.out.println("classLoader.getParent().getParent() = " + classLoader.getParent().getParent());

        try {
            Class<?> clazz = classLoader.loadClass("sun.java2d.Disposer");
            System.out.println("clazz.getCanonicalName() = " + clazz.getCanonicalName());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }


    public static void main(String[] args) {

        getPath();


    }


}
